<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/8 0008
 * Time: 下午 3:55
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\ExpressModel;
use App\Http\Models\ExpressorderModel;
use App\Http\Models\CategoryModel;
use App\Http\Models\RoomModel;
use Validator;
use Session;
use DB;


class RoomController extends Controller
{
    public function index(Request $request, $id)
    {

        $data['data'] = RoomModel::select('room.*', 'category.name')
            ->join('category', 'category.id', '=', 'room.school_id')
            ->where(['school_id' => $id])
            ->paginate(10);
        $data['type'] = 3;
        return view('category.room', compact('data'))->with('id',$id);
    }

    public function add(Request $request, $id)
    {
        if ($request->isMethod('get')) {
            return view('category.roomadd')->with('id', $id);
        } else {
            $this->validate($request, [
                'number_name' => 'required|string|max:100|min:2',
            ]);
            $data = $request->except(['_token', 'uploadfile']);
            $data['school_id'] = $id;
            RoomModel::create($data);
            return redirect('/admin/room/' . $id);
        }
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('get')) {

            $data = RoomModel::where(['id' => $id])->first();
            return view('category.roomedit', compact('data'));
        } else {
            $data = $request->except(['_token', 'uploadfile']);
            $validator = Validator::make($data, [
                'number_name' => 'required|string',
            ]);
            if ($validator->fails()) {
                return redirect('admin/room/edit/' . $id )
                    ->withErrors($validator)
                    ->withInput($data);
            }
            RoomModel::where(['id' => $id])->update($data);
            return redirect('/admin/room/'.$id);
        }

    }
}